
from datetime import datetime, timedelta
from dateutil.relativedelta import relativedelta

from database.user import select_user_by_user_id
from database.weibo import select_weibo_by_release_time, count_activity_by_release_time

from domain.AiInputDataUserInfo import AiInputDataUserInfo
from domain.AiInputDataWeiboInfo import AiInputDataWeiboInfo
from domain.AiInputData import AiInputData



def get_data_for_ai_from_database(user_id):
    # 1.获取用户数据
    result = select_user_by_user_id(user_id)
    if result == None:
        print("未找到数据")
        return None
    user_info_db = AiInputDataUserInfo(**result)
    # print(user_info_db.json())
    # 2.获取近十条微博数据
    recent_weibo = select_weibo_by_release_time(user_id, 10)
    weibi_list = []
    for item in recent_weibo:
        weibo_info_class = AiInputDataWeiboInfo(**item)
        weibi_list.append(weibo_info_class.json())
    
    # 3.统计近一周/一个月的赞、评论、转发数
    now = datetime.now()
    now_date = now.strftime("%Y-%m-%d %H-%M-%S")
    one_week_ago = (now - timedelta(weeks=1)).strftime("%Y-%m-%d %H-%M-%S")
    one_week_ago_statistic = count_activity_by_release_time(user_id, one_week_ago, now_date)
    for key in one_week_ago_statistic.keys():
        one_week_ago_statistic[key] = int(one_week_ago_statistic[key])
        
    one_month_ago = (now - relativedelta(months=1)).strftime("%Y-%m-%d %H-%M-%S")
    one_month_ago_statistic = count_activity_by_release_time(user_id, one_month_ago, now_date)
    for key in one_month_ago_statistic.keys():
        one_month_ago_statistic[key] = int(one_month_ago_statistic[key])
    
    ai_input_data = AiInputData(user_info_db, weibi_list, one_week_ago_statistic, one_month_ago_statistic)
    print(ai_input_data.json())
    